package com.common.frame.net

import com.common.frame.utils.AppContents
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


/**
 *
 * create by sunxy on 2021/2/5
 **/
class RetrofitManager private constructor() {

    companion object{
        val manager by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            RetrofitManager()
        }
    }

    val okHttpClick by lazy {
        OkHttpClient.Builder()
                .connectTimeout(2000, TimeUnit.SECONDS)
                .readTimeout(2000, TimeUnit.SECONDS)
                .writeTimeout(2000, TimeUnit.SECONDS)
//                .addInterceptor(
//                    LoggingInterceptor.Builder()
//                        .loggable(!"release".equals(BuildConfig.BUILD_TYPE))
//                        .setLevel(Level.BODY)
//                        .log(Platform.INFO)
//                        .request("s-Request")
//                        .response("s-Response")
//                        .build()
//                )
                .build()
    }


    val apiService by lazy {
        Retrofit.Builder()
            .baseUrl(AppContents.BASE_URL)
            .client(okHttpClick)
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(ApiService::class.java)
    }

}